Creating
a gambas2
program,
step by step, a telephone index
From
: http://listingambas.blogspot.com/2011/06/anexo-5-optimizando-nuestro-codigo.html
Annex 5: Optimizing our code.
Once
the program written, we see that It can be reorganized
and we reduce repetitive code with
procedure calls.
For example we have some
code beeing several times repeated:
'Put
the
property .Text
of texboxes to blank
PictureBoxFoto.
Picture
=
Picture [ "icon:
/
96/gambas"
]
TextBoxDNI.
text
=
""
TextBoxName.
text
=
""
TextBoxSurname.
text
=
""
TextBoxCompany.
text
=
""
TextBoxPosition.
text
=
""
TextBoxTelCompany.
text
=
""
TextBoxTelPrivate.
text
=
""
TextBoxFax.
text
=
""
TextBoxMobileCompany.
text
=
""
TextBoxMobilePrivate.
text
=
""
TextBoxWEB.
text
=
""
PictureBoxPhoto.
Picture
=
""
TextBoxAddress.
text
=
""
TextBoxComments.
text
=
""
TextBoxDate.
text
=
""
TextBoxMail.
text
=
""
'Write
the
entered data gridviews
title.
fill
()
'The
setfocus
we put right at the beginning of the data
TextBoxDNI.
SetFocus
This code is repeated a lot, we can create a sub-routine and call
it whenever needed.
'-------------------------------------------------
--------------
'Grouping
code
that is repeated many times
'-------------------------------------------------
--------------
PUBLIC
SUB
clean ()
'Put
the
property blank. Text of texbox
PictureBoxFoto.
Picture
=
Picture [ "icon:
/
96/gambas"
]
TextBoxDNI.
text
=
""
TextBoxName.
text
=
""
TextBoxSurname.
text
=
""
TextBoxCompany.
text
=
""
TextBoxPosition.
text
=
""
TextBoxTelCompany.
text
=
""
TextBoxTelPrivate.
text
=
""
TextBoxFax.
text
=
""
TextBoxMobileCompany.
text
=
""
TextBoxMobilePrivate.
text
=
""
TextBoxWEB.
text
=
""
PictureBoxPhoto.
Picture
=
""
TextBoxAddress.
text
=
""
TextBoxComments.
text
=
""
TextBoxDate.
text
=
""
TextBoxMail.
text
=
""
'Write
the
entered data in gridview
title.
fill
()
'Setfocus
right at the beginning of data
TextBoxDNI.
SetFocus
<END
Another improvement would be to add icons to menus:
From the editor menus we put some icons to distinguish menu
options It is easy and good looking:
Another thing would be to put a label where to display that file ".Lis"
we are
currently working on.
In Fmain form ,
create
a TextLabel
called labelActual
. In
the
Gambas
IDE,
we
put the value Raised to
the
the Border property
for a better looking (it is a border around the label box)
Each
time we open, save, or generate "general
list" we would be proposed the name of that file:
For
example: Module Files.fSave.
... .........
lines
&
=
var. data_date
[a] &
Endofline
lines
&
=
var. mail
[a] &
Endofline
NEXT
var.
change
=
"NO"
File . Save
(target, lines)
Fmain.
LabelActual
. text
=
target
ends:
'Cancel button has been
cliked within Dialog.SaveFile() dialog
'End of
the subroutine
END
In the module files.openData
:
....
........
b
+
=
1
var.
mail
[a] =
arr_string [b]
NEXT
var.
change
=
"NO"
title.
fill
()
FMain.
LabelActual
. text
=
sRoot
EndRead:
END
And in the module import.completed
:
... .............
Sline
&
=
var. mail
[a] &
Endofline
NEXT
File . Save
(target, sLine)
var.
change
=
"NO"
FMain.
LabelActual
. text
=
target
'End of
the process
END
We would
get something like this:
Annex
6: Add recent
In
this appendix
we will add a new module called addrecent
, which will take care of "remembering" previously opened files.
First
add menus with the menu editor in
Fmain menu:
And we will add these settings
For
recent1, recent2, recent3
or recent4 names,
we
set the title to "(empty)."
This
way, we
are providing 5 places to store the pathes of the 5 last
opened files.
In
Form Fmain
, we add this to Form_Open ():
PUBLIC
SUB
Form_Open ()
ME.CENTER
()
var. reinit
()
title. define
()
var. path_Picture
=
"icon:
/
96/gambas"
addrecent.tree
()
END
and each time "recent" option menu
is clicked, we open respective file:
'--------------------------------------------
'Open
files according to recent
'--------------------------------------------
PUBLIC
SUB
recent0_Click ()
file. open
(FMain. recent0
. caption
)
END
PUBLIC
SUB
recent1_Click ()
file. open
(FMain. recent1
. caption
)
END
PUBLIC
SUB
recent2_Click ()
file. open
(FMain. recent2
. caption
)
END
PUBLIC
SUB
recent3_Click ()
file. open
(FMain. recent3
. caption
)
END
PUBLIC
SUB
recent4_Click ()
file. open
(FMain. recent4
. caption
)
END
and Form_Close ()
PUBLIC
SUB
Form_Close ()
DIM
res AS
Integer
IF
var. change
=
"yes"
THEN
res =
Message. Question
( "Do
you
want
to
exit without saving?"
, "yes"
, "no"
)
IF
res =
1
THEN
addrecent.fsave ()
ME
. CLOSE
ELSE
STOP
EVENT
'stop
this event and do not leave the program
ENDIF
ELSE
ME
. close
ENDIF
'We do
not leave the program
END
In the module file
, subroutine openData (),
we add at
the
beginning:
PUBLIC
SUB openData (
OPTIONAL sRoot AS String )
DIM a AS Integer
'counter
data
set
DIM b AS Integer
'counter
data
DIM
arr_String AS
String []
DIM
codigofinline AS String
DIM data_number AS Integer
Endofline
=
"|"
IF
sRoot =
""
THEN
Dialog.
Title
= "Select
a
data
file
listin"
Dialog.
Filter
= [ "*. lis" , "Listin
Data" ]
IF
NOT
Dialog. OpenFile () THEN
sRoot =
Dialog. Path
ELSE
STOP
EVENT
ENDIF
ENDIF
arr_String
=
Split
( File
. Load
(sRoot), Endofline )
IF
arr_String [ 0 ]
<> "v0.0.1"
THEN
'version number is not
compatible with this program version, quit the procedure
and
end of the module add
... ...
var. mail
[a] =
arr_String [b]
NEXT
var. change
=
"NO"
title. fill
()
FMain. LabelActual
. text
=
Dialog. Path
addrecents.
add
(Dialog. path
)
finlectura:
END
Now
we define the new module addrecents:
PUBLIC
SUB
add (
path AS
String )
AS
String
IF
FMain. recent0
. caption
=
"(not
recent)"
THEN
FMain. recent0
. caption
=
path
RETURN
ENDIF
'Does
not
attach
if
the element is repeated
IF
FMain. recent0
. caption
=
path THEN
RETURN
ENDIF
IF
FMain. recent1
. caption
=
path THEN
RETURN
ENDIF
IF
FMain. recent2
. caption
=
path THEN
RETURN
ENDIF
IF
FMain. recent3
. caption
=
path THEN
RETURN
ENDIF
IF
FMain. recent4
. caption
=
path THEN
RETURN
ENDIF
IF
FMain. recent1
. caption
=
"(empty)"
THEN
FMain. recent1
. caption
=
path
FMain. recent1
. Visible
=
TRUE
RETURN
ENDIF
IF
FMain. recent2
. caption
=
"(empty)"
THEN
FMain. recent2
. caption
=
path
FMain. recent2
. Visible
=
TRUE
RETURN
ENDIF
IF
FMain. recent3
. caption
=
"(empty)"
THEN
FMain. recent3
. caption
=
path
FMain. recent3
. Visible
=
TRUE
RETURN
ENDIF
IF
FMain. recent4
. caption
=
"(empty)"
THEN
FMain. recent4
. caption
=
path
FMain. recent4
. Visible
=
TRUE
RETURN
ENDIF
'If
you
come
up
here means that we fill the 5 recent
'So
we
must
move
all the latest caption and add the new
FMain. recent0
. caption
=
FMain. recent1
. caption
FMain. recent1
. caption
=
FMain. recent2
. caption
FMain. recent2
. caption
=
FMain. recent3
. caption
FMain. recent3
. caption
=
FMain. recent4
. caption
FMain. recent4
. caption
=
path
END
PUBLIC
SUB
save ()
DIM
lines AS
String
DIM
path AS
String
lines
=
"**************
***
Contents
of
Recent Doc "
&
"\
n"
lines
&
=
"documentosrecents"
&
"\
n"
lines
&
=
"v.0.0.1"
&
"\
n"
lines
&
=
FMain. recent0
. caption
&
"\
n"
lines
&
=
FMain. recent1
. caption
&
"\
n"
lines
&
=
FMain. recent2
. caption
&
"\
n"
lines
&
=
FMain. recent3
. caption
&
"\
n"
lines
&
=
FMain. recent4
. caption
&
"\
n"
lines
&
=
"*************
End
************"
&
"\
n"
path
=
user.home
File
. Save
(
path &
"/
documentsrecentslistin.DocRec"
,
lines )
END
PUBLIC
SUB
open ()
DIM
arr_String AS
String
[]
FMain.
recent1
. Visible
=
FALSE
FMain.
recent2
. Visible
=
FALSE
FMain.
recent3
. Visible
=
FALSE
FMain.
recent4
. Visible
=
FALSE
IF
Exist (user.home &
"/
documentsrecentslistin.DocRec"
) =
FALSE
THEN
'There
is
no
file,
so we can continue with the same name saved
ENDIF
TRY
arr_String =
Split
( File
. LOAD
(
user.home &
"/
documentsrecentslistin.DocRec"
) ,
"\ n" )
IF
ERROR
THEN
Message.
Error
( "error
reading"
)
RETURN
ENDIF
'If
an
error
occurs
returns to the program can not find the file
documentsrecents.DocRec
FMain.
recent0
. caption
=
arr_String [ 3
]
FMain.
recent1
. caption
=
arr_String [ 4
]
FMain.
recent2
. caption
=
arr_String [ 5
]
FMain.
recent3
. caption
=
arr_String [ 6
]
FMain.
recent4
. caption
=
arr_String [ 7
]
IF
FMain. recent1
. caption
<>
"(void)"
THEN
FMain. recent1
. Visible
=
TRUE
ENDIF
IF
FMain. recent2
. caption
<>
"(void)"
THEN
FMain. recent2
. Visible
=
TRUE
ENDIF
IF
FMain. recent3
. caption
<>
"(void)"
THEN
FMain. recent3
. Visible
=
TRUE
ENDIF
IF
FMain. recent4
. caption
<>
"(void)"
THEN
FMain. recent4
. Visible
=
TRUE
ENDIF
END